package com.github.manx98.okdownloader.utils;

import com.github.manx98.okdownloader.event.DownloadTaskEventHandler;
import okhttp3.OkHttpClient;

import java.util.List;

/**
 * 下载任务
 *
 * @author : manx98
 * @date : 2021/5/24
 */
public class DownloadTask {
    private List<DownloadLink> downloadLinks;
    private DownloadTaskEventHandler downloadEventHandler;
    private ProgressHandler progressHandler;
    private DataSaveHandler dataSaveHandler;
    private int bufferSize;
    private DownloadTaskStatus downloadTaskStatus;
    private OkHttpClient client;

    private DownloadTask() {
    }

    /**
     * 创建一个构建器
     *
     * @return 构建器
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    protected OkHttpClient getClient() {
        return client;
    }

    public List<DownloadLink> getDownloadLinks() {
        return downloadLinks;
    }

    /**
     * 获取下载事件处理器
     */
    public DownloadTaskEventHandler getDownloadEventHandler() {
        return downloadEventHandler;
    }

    /**
     * 获取进度存储器
     */
    public ProgressHandler getProgressHandler() {
        return progressHandler;
    }

    /**
     * 获取数据存储处理器
     */
    public DataSaveHandler getDataSaveHandler() {
        return dataSaveHandler;
    }

    /**
     * 获取缓存大小
     *
     * @return 字节数
     */
    public int getBufferSize() {
        return bufferSize;
    }

    /**
     * 获取下载状态
     *
     * @return 下载状态
     */
    public DownloadTaskStatus getDownloadTaskStatus() {
        return downloadTaskStatus;
    }

    /**
     * 下载任务构建器
     */
    public static class Builder {
        private DownloadTaskEventHandler downloadEventHandler = DefaultDownloadEventHandler.instance();
        private int bufferSize = 8192;

        /**
         * 添加下载事件监听(可选的)
         *
         * @param downloadEventHandler 事件监听器
         * @return 构建器
         */
        public Builder downloadEventHandler(DownloadTaskEventHandler downloadEventHandler) {
            this.downloadEventHandler = downloadEventHandler;
            return this;
        }

        /**
         * 缓存大小
         *
         * @param bufferSize 缓存冲区大小
         * @return 构建器
         */
        public Builder bufferSize(int bufferSize) {
            this.bufferSize = bufferSize;
            return this;
        }

        /**
         * 执行构建
         *
         * @param downloadLinks   下载链接列表
         * @param dataSaveHandler 数据存储处理器
         * @param progressHandler 进度存储处理器
         * @param client          OkHttpClient
         * @return 下载任务对象
         */
        public DownloadTask build(List<DownloadLink> downloadLinks, DataSaveHandler dataSaveHandler, ProgressHandler progressHandler, OkHttpClient client) {
            final DownloadTask downloadTask = new DownloadTask();
            downloadTask.downloadLinks = downloadLinks;
            downloadTask.downloadEventHandler = this.downloadEventHandler;
            downloadTask.progressHandler = progressHandler;
            downloadTask.dataSaveHandler = dataSaveHandler;
            downloadTask.bufferSize = this.bufferSize;
            downloadTask.client = client;
            downloadTask.downloadTaskStatus = new DownloadTaskStatus(progressHandler.getFileSize(), progressHandler.getCompletedSize());
            return downloadTask;
        }
    }
}
